package com.xmall.controller.admin;

import com.xmall.bean.vo.BaseRespVo;
import com.xmall.bean.vo.AdminInfoVo;
import com.xmall.bean.vo.DashboardVo;
import com.xmall.service.AdminService;
import com.xmall.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AuthController {

    @Autowired
    AdminService adminService;

    @PostMapping("auth/login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        MallToken token = new MallToken(username, password, "admin");
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        Serializable sessionId = subject.getSession().getId();
        return BaseRespVo.ok(sessionId);
    }

    @RequestMapping("/auth/logout")
    public BaseRespVo logout(HttpSession session) {
        session.invalidate();
        return BaseRespVo.ok();
    }

    @RequestMapping("auth/info")
    public BaseRespVo info(String token){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        AdminInfoVo adminInfoVo = adminService.queryAdminInfoByUsername(username);
        return BaseRespVo.ok(adminInfoVo);
    }


   @RequestMapping("dashboard")
    public BaseRespVo dashboard(Map map){
        DashboardVo dashboardVo = adminService.queryDashBoardTotal();
        return BaseRespVo.ok(dashboardVo);
   }




}
